package cn.geminis.warehouse.service.delivery.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.persistence.AttributeConverter;
import lombok.Getter;

@Getter
public enum DeliveryState {

    PLAN(10, "已计划"),
    LOAD(20, "已装车"),
    TRANSIT(30, "已发出");


    @JsonValue
    private final int value;
    private final String name;

    DeliveryState(int value, String name) {
        this.value = value;
        this.name = name;
    }

    @JsonCreator
    public static DeliveryState fromValue(int value) {
        for (var deliveryState : DeliveryState.values()) {
            if (deliveryState.getValue() == value) {
                return deliveryState;
            }
        }
        throw new IllegalArgumentException();
    }

    public static class DeliveryStateConvert implements AttributeConverter<DeliveryState, Integer> {

        @Override
        public Integer convertToDatabaseColumn(DeliveryState deliveryState) {
            return deliveryState.getValue();
        }

        @Override
        public DeliveryState convertToEntityAttribute(Integer value) {
            return DeliveryState.fromValue(value);
        }
    }
}
